package com.xq.shop.controller;


import com.xq.shop.aop.anno.OperationAnnotation;
import com.xq.shop.dto.*;
import com.xq.shop.service.IUserService;
import com.xq.shop.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import utils.BasePage;
import utils.BaseResult;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author gaoxiaojin
 * @since 2024-07-16
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    private final IUserService userService;

    @Autowired
    public UserController(IUserService userService) {
        this.userService = userService;
    }

    @RequestMapping("/login")
    public BaseResult<String> login(@RequestBody @Validated LoginDto dto) {
        log.info("用户登录----入参：{}", dto);
        BaseResult<String> result = new BaseResult<>();
        String token = userService.login(dto);
        log.info("登录成功----出参：{}", token);
        result.initializeSuccess(token);
        return result;
    }

    @RequestMapping("/getInfo")
    public BaseResult<UserVo> getInfo(HttpServletRequest request) {
        BaseResult<UserVo> result = new BaseResult<>();
        UserVo vo = userService.getInfo(request);
        result.initializeSuccess(vo);
        return result;
    }

    @RequestMapping("/logout")
    public BaseResult<Void> logout(HttpServletRequest request) {
        BaseResult<Void> result = new BaseResult<>();
        userService.logout(request);
        result.initializeSuccess();
        return result;
    }

    @RequestMapping("/edit")
    @OperationAnnotation(function = "编辑用户")
    public BaseResult<Void> editUser(@RequestBody @Validated EditUserDto dto, HttpServletRequest request) {
        UserVo vo = userService.getInfo(request);
        BaseResult<Void> result = new BaseResult<>();
        userService.editUser(dto, vo.getId());
        result.initializeSuccess();
        return result;
    }

    @RequestMapping("/page")
    public BaseResult<BasePage<UserVo>> page(@RequestBody QueryDto dto, HttpServletRequest request) {
        UserVo userVo = userService.getInfo(request);
        BaseResult<BasePage<UserVo>> result = new BaseResult<>();
        BasePage<UserVo> page = userService.pageUser(dto, userVo);
        result.initializeSuccess(page);
        return result;
    }

    @RequestMapping("/remove")
    @OperationAnnotation(function = "移除用户")
    public BaseResult<Void> remove(@RequestBody @Validated IntegerDto dto, HttpServletRequest request) {
        UserVo userVo = userService.getInfo(request);
        BaseResult<Void> result = new BaseResult<>();
        userService.removeUser(dto, userVo.getId());
        result.initializeSuccess();
        return result;
    }

    @RequestMapping("/changeInfo")
    @OperationAnnotation(function = "编辑个人信息")
    public BaseResult<Void> changeInfo(@RequestBody @Validated ChangeInfoDto dto, HttpServletRequest request) {
        userService.getInfo(request);
        BaseResult<Void> result = new BaseResult<>();
        userService.changeInfo(dto, request);
        result.initializeSuccess();
        return result;
    }
}
